package common;

import java.io.Serializable;
import java.util.LinkedList;

import common.adventurecard.AdventureCardsList;

public class TileEvent implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final Game game;
	private final LinkedList<AdvCard> allAdvCards;

	public TileEvent(final Game sg) {
		this.game = sg;
		this.allAdvCards = AdventureCardsList.initAllAdvCardList();
	}

	public AdvCard randomAdventureCard() {
		// System.out.println("Inne i randomAdventureCard");
		final int randomCard = (int) (Math.random() * this.allAdvCards.size());
		return this.allAdvCards.get(randomCard);
	}

	// Cards Must be sorted before calling this method.
	// If they are not, the game will continue, but the cards will be played
	// in the wrong order.
	// Returns true if more cards should be played.
	public boolean playCards(final LinkedList<AdvCard> cardsOnThisTileList,
			final int player) {

		if (!cardsOnThisTileList.isEmpty()) {
			// If doStuff returns false, we should not play any more cards
			final boolean continueToPlay = cardsOnThisTileList.getFirst()
					.doStuff(this.game);
			// server.update(player, this.game);
			if (!continueToPlay) {
				return false;
			}
			cardsOnThisTileList.removeFirst();
			// We should now check if there are more cards on the tile.
			// If not, we should return false.
			if (cardsOnThisTileList.size() == 0) {
				return false;
			}
		}
		return true;
	}
}
